package com.andreas.persistence;

import com.andreas.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * 描述：持久化队列的Sender发送者 发送者1
 * 消息的可靠性是RabbitMQ的一大特色，那么RabbitMQ是如何避免信息丢失？
 * 1.消息的ACK确认机制，可以防止消费者丢失信息
 * 2.万一在消费之前，RabbitMQ服务器宕机了，那么消息也会丢失。
 * 想要将消息持久化，那么路由和队列都要持久化才可以。
 */
public class Sender {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare("test_exchange_topic", "topic", true);
        String msg = "商品降价";
        channel.basicPublish("test_exchange_topic", "product.price", MessageProperties.PERSISTENT_TEXT_PLAIN, msg.getBytes(StandardCharsets.UTF_8));
        System.out.println("[用户系统]：" + msg);
        channel.close();
        connection.close();
    }
}
